package com.dsteiner.widgets.client.controller;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import com.dsteiner.widgets.client.NineBoxWidget.NineBoxModel;
import com.dsteiner.widgets.client.Widgets.ILazyWidget;
import com.dsteiner.widgets.client.Widgets.ISimplePanel;
import com.dsteiner.widgets.client.Widgets.IWidget;
import com.dsteiner.widgets.client.Widgets.SimplePanelExt;
import com.dsteiner.widgets.client.Widgets.WidgetListenerAdapter;
import com.dsteiner.widgets.client.resources.NineBoxCssResource;

public class NineBoxController {

	private NineBoxModel nineBoxModel;
	private ISimplePanel contentPanelHolder;
	private ILazyWidget nineBoxWidget;
	private IWidget topLeft;
	private IWidget topRight;
	private IWidget top;
	private IWidget bottomLeft;
	private IWidget bottomRight;
	private IWidget bottom;
	private IWidget left;
	private IWidget right;

	public void registerBottom(IWidget bottom) {
		this.bottom = bottom;
	}
	public void registerBottomLeft(IWidget bottomLeft) {
		this.bottomLeft = bottomLeft;
	}
	public void registerBottomRight(IWidget bottomRight) {
		this.bottomRight = bottomRight;
	}
	public void registerContentPanelHolder(ISimplePanel contentPanelHolder) {
		this.contentPanelHolder = contentPanelHolder;
	}
	public void registerLeft(IWidget left) {
		this.left = left;
	}
	public void registerNineBoxModel(NineBoxModel nineBoxModel) {
		this.nineBoxModel = nineBoxModel;
		nineBoxModel.addPropertyChangeListener("nineBoxCssResource", new PropertyChangeListener(){
			public void propertyChange(PropertyChangeEvent evt) {
				updateStyleNames();
			}
		});
		nineBoxModel.addPropertyChangeListener("contentWidget", new PropertyChangeListener(){
			public void propertyChange(PropertyChangeEvent evt) {
				updateContentWidget();
			}
		});
	}
	public void registerNineBoxWidget(ILazyWidget nineBoxWidget) {
		this.nineBoxWidget = nineBoxWidget;
		nineBoxWidget.addWidgetListener(new WidgetListenerAdapter() {
			@Override
			public void onAttach(IWidget widget) {
				updateContentWidget();
				updateStyleNames();
			}
		});
	}
	public void registerRight(IWidget right) {
		this.right = right;
	}

	public void registerTop(IWidget top) {
		this.top = top;
	}

	public void registerTopLeft(IWidget topLeft) {
		this.topLeft = topLeft;
	}

	public void registerTopRight(IWidget topRight) {
		this.topRight = topRight;
	}

	private void updateContentWidget() {
		IWidget contentWidget = nineBoxModel.getContentWidget();
		contentPanelHolder.setWidget(contentWidget);
	}

	private void updateStyleNames() {
		NineBoxCssResource nineBoxCssResource = nineBoxModel.getNineBoxCssResource();
		nineBoxWidget.setStyleName(nineBoxCssResource.contentArea());
		topLeft.setStyleName(nineBoxCssResource.contentAreaTopLeftBorder());
		topRight.setStyleName(nineBoxCssResource.contentAreaTopRightBorder());
		top.setStyleName(nineBoxCssResource.contentAreaTopBorder());
		right.setStyleName(nineBoxCssResource.contentAreaRightBorder());
		left.setStyleName(nineBoxCssResource.contentAreaLeftBorder());
		bottomLeft.setStyleName(nineBoxCssResource.contentAreaBottomLeftBorder());
		bottomRight.setStyleName(nineBoxCssResource.contentAreaBottomRightBorder());
		bottom.setStyleName(nineBoxCssResource.contentAreaBottomBorder());
		contentPanelHolder.setStyleName(nineBoxCssResource.contentAreaHolder());
		
	}
}